/*
 * @Author: Leo.wang wanglizhigs@163.com
 * @Date: 2024-05-12 10:27:04
 * @LastEditors: Leo.wang wanglizhigs@163.com
 * @LastEditTime: 2024-05-12 20:37:35
 * @FilePath: /gptfront-dev/src/components/ConversationGroup/conversationGroup.hooks.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { ref, computed, onMounted, nextTick, onUnmounted } from 'vue'
import { useDebounceFn } from '@vueuse/core'
import { useStore } from 'vuex'

export function useScroll() {
  const store = useStore()
  const hasNextPage = computed(() => store.state.chat.hasNextPage)
  const currentRoomId = computed(() => store.state.chat.currentRoomId)
  const scrollTop = ref(0)
  const isFetchingNextPage = ref(false)
  let currentPage = 0

  const scrollTopAction = useDebounceFn(async () => {
    if (!hasNextPage.value || isFetchingNextPage.value) {
      return
    }
    isFetchingNextPage.value = true
    currentPage++
    await store.dispatch('chat/fetchHistoryNextPage', {
      roomId: currentRoomId.value,
      page: currentPage,
    })
    isFetchingNextPage.value = false
  })

  // #ifdef WEB
  function handleOnWheel(e) {
    if (e.deltaY === 0) {
      return
    }
    const scrollTopVal = scrollTop.value - e.deltaY
    const query = uni.createSelectorQuery()
    query.select('.chat-scroll-view').boundingClientRect()
    query.select('.scroll-view-content').boundingClientRect()
    query.exec((res) => {
      const scrollHeight = res[1].height - res[0].height + 12
      scrollTop.value =
        scrollTopVal < 0
          ? 0
          : scrollTopVal > scrollHeight
            ? scrollHeight
            : scrollTopVal
    })
    e.preventDefault()
  }
  onMounted(async () => {
    await nextTick()
    document
      .querySelector('.chat-scroll-view .uni-scroll-view')
      ?.addEventListener('wheel', handleOnWheel, { passive: false })
  })
  onUnmounted(() => {
    document
      .querySelector('.chat-scroll-view .uni-scroll-view')
      ?.removeEventListener('wheel', handleOnWheel, { passive: false })
  })
  // #endif

  return {
    scrollTop,
    hasNextPage,
    currentRoomId,
    isFetchingNextPage,
    scrollTopAction,
  }
}

export function useConversations() {
  const store = useStore()

  const conversations = computed(() => {
    const history = store.state.chat.history
    console.log('history', history)
    // const map = new Map()
    // history.reverse().forEach((item, index) => {
    //   if (item.role === 'user') {
    //     if (!map.has(item.message)) {
    //       map.set(item.message, [item])
    //     }
    //   }
    //   if (item.role === 'assistant') {
    //     const result = history[index - 1]
    //     if(result?.message) {
    //       map.set(result.message, [
    //         ...map.get(result.message) || [],
    //         item,
    //       ])
    //     }
    
    //   }
    // })
    return history
  })

  return {
    conversations,
  }
}
